package com.bw.user.mvvm.viewmodel;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ToastUtils;

import com.bw.mvvm_common.dao.UserBean;
import com.bw.mvvm_common.dao.UserBeanDao;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.utils.GreenDaoUtils;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.user.mvvm.model.LoginModel;
import com.bw.user.mvvm.model.entity.LoginEntity;
import com.bw.user.mvvm.view.fragment.RegisterActivity;


import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/19 0019 下午 3:57
 *
 */
class LoginViewModel extends BaseViewModel<LoginModel> {
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> pwd = new ObservableField<>();


    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    protected void result(BaseEntity entity) {
        LoginEntity loginEntity = (LoginEntity) entity;

        if (loginEntity.getMessage().equals("登录成功")){
            UserBeanDao green = GreenDaoUtils.getGreenDaoUtils().green();
            UserBean userBean = new UserBean();
            userBean.setUser_id(loginEntity.getData().getId());
            userBean.setUser_mobile(loginEntity.getData().getUser_mobile());
            green.insert(userBean);
//            EventBus.getDefault().postSticky(phone.get());
            finishPage();

        }else{
            ToastUtils.showLong("登录sb");
        }
    }
    //返回按钮
    public void finish(){
        finishPage();
    }
    public void register(){
        HashMap<String,Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ, RegisterActivity.class);
        startPage(map);
    }
    public void login(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("mobile",phone.get());
        map.put("pushId","1");
        map.put("pwd",pwd.get());
        m.requestLogin(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
//        ToastUtils.showLong(phone.get()+pwd.get());

    }
    public void forget(){
        ToastUtils.showLong("forget");
    }
}
